package com.tstudy.laoshibang.utils;

import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.CONSTANT;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

public class SharedPreferencesUtil {
	
	public static final String QZONE_OAUTH_INFO  = "qzone_oauth_info";
	public static final String WECHAT_OAUTH_INFO  = "wechat_oauth_info";
	private static final String ACCOUNT_PREFERENCES = "account_preferences"; // 账户相关 Preferences文件名
	private static final String COMMON_PREFERENCES = "common_preferences"; // 通用Preferences文件名
	private static final String SAVE_PHOTO = "SAVE_PHOTO";
	private static final String MY_ACCOUNT = "MY_ACCOUNT"; //
	private static final String LOGIN_TYPE = "login_type";
	private static final String MY_PASSWORD = "MY_PASSWORD";
	private static final String WEIBO_USER_INFO = "weibo_user_info";
	private static final String WECHAT_USER_INFO = "wechat_user_info";
	private static final String MESSAGE_COUNT = "message_count";
	private static final String CLIENT_ID = "client_id";
	private static final String GROUP_ID = "group_id";
	private static final String IS_FIRST_OPEN = "is_first_open";
	private static final String ENCRYPT_SEED = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzIlovefashion";
	public static void saveQzoneAuth(String accessToken,String openId,String expiresIn){
		SharedPreferences sp = BaseApplication.mContext.getSharedPreferences(QZONE_OAUTH_INFO,Context.MODE_APPEND);
		SharedPreferences.Editor edit = sp.edit();
		edit.putString(CONSTANT.OAUTH.ACCESS_TOKEN, accessToken);
		edit.putString(CONSTANT.OAUTH.OPEN_ID,openId);
		edit.putString(CONSTANT.OAUTH.EXPIRES_IN, expiresIn);
		edit.putString(CONSTANT.OAUTH.BIND_TIME, "" + System.currentTimeMillis());
		edit.commit();
	}
	
	public static String getQzoneInfo(String key){
		SharedPreferences sp = BaseApplication.mContext.getSharedPreferences(QZONE_OAUTH_INFO,Context.MODE_APPEND);
		return sp.getString(key, "");
	}
	
	
	public static void saveWechatAuthInfo(String accessToken,String openId,String expiresIn,String refreshToken){
		SharedPreferences sp = BaseApplication.mContext.getSharedPreferences(WECHAT_OAUTH_INFO,Context.MODE_APPEND);
		SharedPreferences.Editor edit = sp.edit();
		edit.putString(CONSTANT.OAUTH.ACCESS_TOKEN, accessToken);
		edit.putString(CONSTANT.OAUTH.OPEN_ID,openId);
		edit.putString(CONSTANT.OAUTH.REFRESH_TOKEN, refreshToken);
		edit.putString(CONSTANT.OAUTH.EXPIRES_IN, expiresIn);
		edit.putString(CONSTANT.OAUTH.BIND_TIME, "" + System.currentTimeMillis() + expiresIn);
		edit.commit();
		
	}
	public static String getWechatAuthInfo(String key){
		SharedPreferences sp = BaseApplication.mContext.getSharedPreferences(WECHAT_OAUTH_INFO,Context.MODE_APPEND);
		return sp.getString(key, "");
	}
	public static boolean isTokenExpried(String name){
		SharedPreferences sp = BaseApplication.mContext.getSharedPreferences(name,Context.MODE_APPEND);
		String expriesIn =  sp.getString(CONSTANT.OAUTH.EXPIRES_IN, "0");
		String bindTime = sp.getString(CONSTANT.OAUTH.BIND_TIME,"0");
		long expries = Long.valueOf(expriesIn) + Long.valueOf(bindTime);
		return CommonUtil.checkExpried(Long.valueOf(expries));
	}

	public static void clear(String name){
		SharedPreferences sp = BaseApplication.mContext.getSharedPreferences(name,Context.MODE_APPEND);
		SharedPreferences.Editor edit = sp.edit();
		edit.clear();
		edit.commit();
	}
	
	public static void saveWeiboUseInfo(String weiboUserInfo){
		SharedPreferences sp = BaseApplication.mContext.getSharedPreferences(ACCOUNT_PREFERENCES, Context.MODE_APPEND);
		Editor editor = sp.edit();
		editor.putString(WEIBO_USER_INFO,weiboUserInfo);
		editor.commit();
	}
	
	public static String getWeiboUserInfo(){
		SharedPreferences sp = BaseApplication.mCurrentActivity.getSharedPreferences(ACCOUNT_PREFERENCES, Context.MODE_APPEND);
		return  sp.getString(WEIBO_USER_INFO, "");
	}
	
	public static void saveWechatUseInfo(String wechatUserInfo){
		SharedPreferences sp = BaseApplication.mCurrentActivity.getSharedPreferences(ACCOUNT_PREFERENCES, Context.MODE_APPEND);
		Editor editor = sp.edit();
		editor.putString(WECHAT_USER_INFO,wechatUserInfo);
		editor.commit();
	}
	
	public static String getWechatUserInfo(){
		SharedPreferences sp = BaseApplication.mCurrentActivity.getSharedPreferences(ACCOUNT_PREFERENCES, Context.MODE_APPEND);
		return  sp.getString(WECHAT_USER_INFO, "");
	}
	
	public static void saveMessageCount(Context context,int count){
		SharedPreferences sp = context.getSharedPreferences(ACCOUNT_PREFERENCES, Context.MODE_APPEND);
		Editor editor = sp.edit();
		editor.putInt(MESSAGE_COUNT,count);
		editor.commit();
	}
	
	public static int getMessageCount(Context context){
		SharedPreferences sp = context.getSharedPreferences(ACCOUNT_PREFERENCES, Context.MODE_APPEND);
		return  sp.getInt(MESSAGE_COUNT,0);
	}
	
	public static void saveClientId(Context context,String clientId){
		SharedPreferences sp = context.getSharedPreferences(COMMON_PREFERENCES, Context.MODE_APPEND);
		Editor editor = sp.edit();
		editor.putString(CLIENT_ID,clientId);
		editor.commit();
	}
	
	public static String getClientId(Context context){
		SharedPreferences sp = context.getSharedPreferences(COMMON_PREFERENCES, Context.MODE_APPEND);
		return  sp.getString(CLIENT_ID, "");
	}
	
	public static void saveGroupId(Context context,String groupId){
		SharedPreferences sp = context.getSharedPreferences(COMMON_PREFERENCES, Context.MODE_APPEND);
		Editor editor = sp.edit();
		editor.putString(GROUP_ID,groupId);
		editor.commit();
	}
	
	public static String getGroupId(Context context){
		SharedPreferences sp = context.getSharedPreferences(COMMON_PREFERENCES, Context.MODE_APPEND);
		return  sp.getString(GROUP_ID, "");
	}
	
	public static void setSavePhoto(boolean isSave) {
		SharedPreferences sp = BaseApplication.mCurrentActivity.getSharedPreferences(ACCOUNT_PREFERENCES, Context.MODE_APPEND);
		// 使用Editor接口修改SharedPreferences中的值并提交�?
		Editor editor = sp.edit();
		editor.putBoolean(SAVE_PHOTO, isSave);
		editor.commit();
	}
	public static boolean getSavePhoto() {
		SharedPreferences sp = BaseApplication.mCurrentActivity.getSharedPreferences(ACCOUNT_PREFERENCES, Context.MODE_APPEND);
		return sp.getBoolean(SAVE_PHOTO, false);
	}
	
	public static void clearAccount() {
		SharedPreferences pref = BaseApplication.mCurrentActivity.getSharedPreferences(ACCOUNT_PREFERENCES, Context.MODE_APPEND);
		Editor editor = pref.edit();
		editor.clear();
		editor.commit();
	}
	

	public static String getLoginAccount() {
		SharedPreferences sp = BaseApplication.mCurrentActivity.getSharedPreferences(ACCOUNT_PREFERENCES, Context.MODE_APPEND);
		return sp.getString(MY_ACCOUNT, "");
	}
	
	public static String getLoginPassword() {
		SharedPreferences sp = BaseApplication.mCurrentActivity.getSharedPreferences(ACCOUNT_PREFERENCES, Context.MODE_APPEND);
		String password = sp.getString(MY_PASSWORD, "");
		// 对密码进行AES解密
		if (!TextUtils.isEmpty(password)) {
			try {
				password = AESEncryptor.decrypt(ENCRYPT_SEED, password);
			} catch (Exception ex) {
				LogUtil.d("ex.getmessage" + ex.getMessage());
				password = "";
			}
		}
		return password;
	}
	
	public static void setFirstOpen(boolean isFirst) {
		SharedPreferences sp = BaseApplication.mCurrentActivity.getSharedPreferences(COMMON_PREFERENCES, Context.MODE_APPEND);
		Editor editor = sp.edit();
		editor.putBoolean(IS_FIRST_OPEN, isFirst);
		editor.commit();
	}
	public static boolean isFirstOpen() {
		if (BaseApplication.mContext != null) {
			SharedPreferences sp = BaseApplication.mContext.getSharedPreferences(COMMON_PREFERENCES, Context.MODE_APPEND);
			return sp.getBoolean(IS_FIRST_OPEN, true);
		}
		return false;
	}
	
	public static int getLoginType() {
		SharedPreferences sp = BaseApplication.mCurrentActivity.getSharedPreferences(ACCOUNT_PREFERENCES, Context.MODE_APPEND);
		return  sp.getInt(LOGIN_TYPE, -1);
	}

}
